package se.webbzon.boltzmann.game.object.dialogue.word;

import javax.media.j3d.Transform3D;
import javax.vecmath.Vector3d;

import se.webbzon.boltzmann.game.object.dialogue.layout.DialogueLayout;
import se.webbzon.oschi01.worldobject.appearance.Animation3D;
import se.webbzon.oschi01.worldobject.appearance.Appearance3D;

public class OscillatingDialogueWord extends DividedDialogueWord {
	
	// The phase difference per step
	private static final double dx = Math.PI/15;
	
	// The current phase of the oscillation
	private double x;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new oscillating dialogue word with the default color. **/
	public OscillatingDialogueWord(DialogueLayout layout, String word) {
		super(layout,word);
		x = 0;
		getAppearance().setAnimation(new OscillationAnimation());
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class OscillationAnimation implements Animation3D {

		@Override public void onStepEnd(Appearance3D appearance) {
			x += dx;
			final int length = getWord().length();
			final DialogueLayout layout = getLayout();
			final double width = layout.getCharacterWidth();
			final double height = layout.getCharacterHeight();
			
			final Transform3D transform = new Transform3D();
			final Vector3d translation = new Vector3d();
			
			for (int i = 0; i < length; i++) {
				final DividedDialogueCharacter character = getCharacter(i);
				translation.set(width * i, 
								0.25 * height * Math.sin(x + i * dx), 
								0);
				character.getTransform(transform);
				transform.setTranslation(translation);
				character.setTransform(transform);
			}
		}
		
	}

}
